package com.shop.loan.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.loan.mapper.LoanOrderBillByMybatisMapper;
import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.mapper.LoanOrderPayLogByMybatisMapper;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.LoanOrderBillByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.entity.SettingByMybatis;
import com.shop.loan.model.enums.OpFailType;
import com.shop.loan.model.enums.PayType;
import com.shop.loan.model.vo.PayOrderByOfflineVo;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.util.LoanOrderUtil;
import com.shop.loan.util.OpFailUtil;
import com.shop.loan.util.PasswordUtil;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.OperationHandler;
import xyz.erupt.upms.service.EruptUserService;
import xyz.erupt.upms.util.IpUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Service
public class OfflinePayHandler implements OperationHandler<Object, PayOrderByOfflineVo> {

    @Resource
    LoanOrderUtil loanOrderUtil;

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    LoanOrderBillByMybatisMapper loanOrderBillByMybatisMapper;

    @Resource
    EruptUserService eruptUserService;

    @Resource
    OpFailUtil opFailUtil;

    @Resource
    private HttpServletRequest request;

    @Resource
    RedisCache redisCache;

    @Resource
    LoanOrderPayLogByMybatisMapper loanOrderPayLogByMybatisMapper;

    @Override
    public String exec(List<Object> data, PayOrderByOfflineVo payOrderByOfflineVo, String[] param) {
        Object item = data.get(0);
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(item));
        Long id = jsonObject.getLongValue("id");

        /**
         * 校验还款密码
         */
        SettingByMybatis settingByMybatis = redisCache.getSetting(false);
        if (!PasswordUtil.checkPassword(payOrderByOfflineVo.getPassword(), settingByMybatis.getPayPassword())) {
            opFailUtil.opFailOp(IpUtil.getIpAddr(request), eruptUserService.getCurrentUid(), OpFailType.pay);
            return "msg.error('无效还款密码!')";
        }
        payOrderByOfflineVo.setOrderId(id);
        payOrderByOfflineVo.setOnline(false);
        ResResult payRes = loanOrderUtil.orderPay(payOrderByOfflineVo);
        if (payRes.getCode().intValue() != Code.SUCCESS.getCode()) {
            return "msg.error('" + payRes.getMsg() + "')";
        }
        return null;
    }


    @Override
    public PayOrderByOfflineVo eruptFormValue(List<Object> data, PayOrderByOfflineVo payOrderByOfflineVo, String[] param) {
        Object item = data.get(0);
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(item));
        Long id = jsonObject.getLongValue("id");

        LoanOrderByMybatis loanOrderByMybatis = loanOrderByMybatisMapper.selectByPrimaryKey(id);
        LoanOrderBillByMybatis loanOrderBillByMybatis = loanOrderBillByMybatisMapper.selectFirstUnPay(id);

        if (loanOrderByMybatis == null || loanOrderBillByMybatis == null) {
            return payOrderByOfflineVo;
        }

        BigDecimal periodPayMoney = loanOrderPayLogByMybatisMapper.selectSumByBillId(loanOrderBillByMybatis.getId(), null, PayType.principal_interest.getType());
        periodPayMoney = periodPayMoney == null ? BigDecimal.ZERO : periodPayMoney;

        payOrderByOfflineVo.setTotalMoney(loanOrderByMybatis.getMoney().add(loanOrderByMybatis.getRateMoney()).add(loanOrderByMybatis.getShopMoney()));
        payOrderByOfflineVo.setPayMoney(loanOrderByMybatis.getPayMoney());
        payOrderByOfflineVo.setPeriod(loanOrderBillByMybatis.getPeriod());
        payOrderByOfflineVo.setTotalPeriod(loanOrderBillByMybatis.getTotalPeriod());
        payOrderByOfflineVo.setPeriodMoney(loanOrderBillByMybatis.getMoney());
        payOrderByOfflineVo.setPeriodRateMoney(loanOrderBillByMybatis.getRateMoney());
        payOrderByOfflineVo.setPeriodPayMoney(periodPayMoney);
        payOrderByOfflineVo.setPeriodDiscountMoney(loanOrderBillByMybatis.getDiscountsMoney() == null ? BigDecimal.ZERO : loanOrderBillByMybatis.getDiscountsMoney());
        loanOrderBillByMybatis.setPayMoney(periodPayMoney);
        payOrderByOfflineVo.setPeriodUnMoney(Util.getUnPayMoneyByPi(loanOrderBillByMybatis));
        payOrderByOfflineVo.setShopMoney(loanOrderByMybatis.getShopMoney());
        payOrderByOfflineVo.setPayMoneyByGoods(loanOrderByMybatis.getShopMoney());
        return payOrderByOfflineVo;
    }
}
